const productListEle = document.getElementById('productList');


getProductsData();



// 请求数据
function getProductsData() {
    $.ajax({
        type: 'POST',
        url: 'http://nocat.life:3021/product/find',
        data: {
            pageSize: 12
        },
        success(res) {
            if (res.code == 200) {
                productsRender(res.data.rows);
            }
        }
    })
}

// 渲染数据
function productsRender(productsData) {
    productListEle.innerHTML = productsData.map(item => {
        return (
            `
            <div data-id="${item._id}" class="list-item">
                <img data-id="${item._id}" class="item-image" src="${item.list_image}" alt="">
                <p data-id="${item._id}" class="item-name">${item.name}</p>
                <strong data-id="${item._id}" class="item-price">￥${item.price}</strong>
            </div>
            `
        )
    }).join("")
}


productListEle.onclick = function (e) {
    const id = e.target.dataset.id;
    if (id) {
        // location.href = './detail.html?_id=' + id;
        localStorage._id = e.target.dataset.id;
        sessionStorage._id = e.target.dataset.id;
        location.href = './detail.html';
    }
}